import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class ClusterTestJava {
    private static final String SERVER_ADDR_1 = "127.0.0.1:8848";
    private static final String SERVER_ADDR_2 = "127.0.0.1:8849";
    private static final String SERVER_ADDR_3 = "127.0.0.1:8850";
    private static final String SERVER_ADDR_4 = "127.0.0.1:8851";
    
    public static void main(String[] args) {
        System.out.println("☕ Java Nacos集群客户端测试");
        System.out.println("=".repeat(50));
        
        try {
            // 创建两个客户端，连接不同节点
            Properties props1 = new Properties();
            props1.put("serverAddr", SERVER_ADDR_1);
            props1.put("namespace", "public");
            
            Properties props2 = new Properties();
            props2.put("serverAddr", SERVER_ADDR_2);
            props2.put("namespace", "public");
            
            NamingService client1 = NacosFactory.createNamingService(props1);
            NamingService client2 = NacosFactory.createNamingService(props2);
            
            ConfigService config1 = NacosFactory.createConfigService(props1);
            ConfigService config2 = NacosFactory.createConfigService(props2);
            
            System.out.println("🎯 Java客户端1连接节点1 (" + SERVER_ADDR_1 + ")");
            System.out.println("🎯 Java客户端2连接节点2 (" + SERVER_ADDR_2 + ")");
            
            // 测试1: 客户端1注册服务
            System.out.println("\n🧪 测试1: 客户端1注册服务");
            Instance instance1 = new Instance();
            instance1.setIp("127.0.0.1");
            instance1.setPort(9001);
            instance1.setServiceName("java-service-1");
            instance1.setHealthy(true);
            client1.registerInstance("java-service-1", instance1);
            System.out.println("✅ 客户端1注册java-service-1成功");
            
            // 测试2: 客户端2注册服务
            System.out.println("\n🧪 测试2: 客户端2注册服务");
            Instance instance2 = new Instance();
            instance2.setIp("127.0.0.1");
            instance2.setPort(9002);
            instance2.setServiceName("java-service-2");
            instance2.setHealthy(true);
            client2.registerInstance("java-service-2", instance2);
            System.out.println("✅ 客户端2注册java-service-2成功");
            
            // 测试3: 检查集群发现
            System.out.println("\n🧪 测试3: 检查集群节点发现");
            Thread.sleep(2000);
            
            // 客户端1查看所有服务
            List<Instance> services1 = client1.getAllInstances("java-service-1");
            List<Instance> services2 = client2.getAllInstances("java-service-2");
            
            System.out.println("📊 客户端1发现的服务实例:");
            for (Instance instance : services1) {
                System.out.println("   🏷️  " + instance.getIp() + ":" + instance.getPort() + " (" + instance.getServiceName() + ")");
            }
            
            System.out.println("📊 客户端2发现的服务实例:");
            for (Instance instance : services2) {
                System.out.println("   🏷️  " + instance.getIp() + ":" + instance.getPort() + " (" + instance.getServiceName() + ")");
            }
            
            // 测试4: 配置共享
            System.out.println("\n🧪 测试4: 配置共享测试");
            config1.publishConfig("java-shared-config", "DEFAULT_GROUP", "shared-by-java-client1");
            System.out.println("✅ 客户端1发布共享配置");
            
            Thread.sleep(1000);
            
            String configValue = config2.getConfig("java-shared-config", "DEFAULT_GROUP", 3000);
            System.out.println("📝 客户端2获取共享配置: " + configValue);
            
            // 测试5: 故障转移测试
            System.out.println("\n🧪 测试5: 故障转移测试");
            System.out.println("⚠️  请手动停止一个节点，然后按回车继续...");
            
            Scanner scanner = new Scanner(System.in);
            scanner.nextLine();
            
            // 测试故障转移后的功能
            try {
                Instance testInstance = new Instance();
                testInstance.setIp("127.0.0.1");
                testInstance.setPort(9003);
                testInstance.setServiceName("test-after-failover");
                testInstance.setHealthy(true);
                client1.registerInstance("test-after-failover", testInstance);
                System.out.println("✅ 故障转移后注册成功 - 集群正常工作");
            } catch (Exception e) {
                System.out.println("❌ 故障转移后注册失败: " + e.getMessage());
            }
            
            // 测试6: 跨节点发现
            System.out.println("\n🧪 测试6: 跨节点发现测试");
            List<Instance> allInstances = client1.getAllInstances("java-service-2");
            System.out.println("客户端1能否发现客户端2注册的服务: " + (allInstances.size() > 0 ? "✅ 可以" : "❌ 不可以"));
            
            System.out.println("\n🎯 Java集群测试完成！");
            
            // 清理测试数据
            try {
                client1.deregisterInstance("java-service-1", "127.0.0.1", 9001);
                client2.deregisterInstance("java-service-2", "127.0.0.1", 9002);
                client1.deregisterInstance("test-after-failover", "127.0.0.1", 9003);
                config1.removeConfig("java-shared-config", "DEFAULT_GROUP");
            } catch (Exception e) {
                // 忽略清理错误
            }
            
        } catch (Exception e) {
            System.err.println("❌ 测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}